<template>
	<view class="container">
		<view :style="'height:'+statusbarH+'px'"></view>
		<!-- <nav-ber :title="'任务广场'" :bgColor="'#f7f8ff'"></nav-ber> -->
		<!-- <view class="u-search-flex">
			<u-icon @click="gotoproFun" name="map-fill" color="#2979ff"></u-icon>
			<span @click="gotoproFun" class="addr_font">北京</span>
			<u-search placeholder="搜索任务名称" v-model="keyword" :showAction="false" shape="round"></u-search>
		</view> -->
		<nav-ber :title="'我的代表'" :bgColor="'#f7f8ff'"></nav-ber>
		<view style="padding: 0 20rpx;box-sizing: border-box;display: flex;align-items: center;">
			<view style="flex: 1">
				<u-subsection @change="sectionChange" :list="list" mode="subsection" :current="current"></u-subsection>
			</view>
			
			<view  style="width: 30rpx;margin: 0 10rpx;" @click="rightFun">
				<u-icon name="list-dot" color="#2979ff" size="24"></u-icon>
			</view>
		</view>
		<view class="province">
			<scroll-view scroll-y="true" style="height: 100%" bindscrolltoupper="upper" @scrolltolower="lower" :lower-threshold="300">
				<view class="items_box" v-for="(item, index2) in appListMin" :key="index2" @tap="viewTasksFun(current, item)">
					<view class="font_w" id="font_color">
						<view>
							<span>{{ item.hallName }}</span>
							<span>{{ item.province }}</span>
						</view>
						<view style="font-weight: 900;margin-right:20rpx;color: #111;display: flex;align-items: center;margin-bottom: 20rpx;">
							{{ statusAll[item.status] }}
						</view>
					</view>
					<view class="time_flex">
						<view>
							<view class="mar_20" style="color: #111;font-weight: 600;">{{ item.hospital }} </view>
							<view class="mar_20" v-if="item.type != 3 && item.type != 4 && item.researchObject != 1" style="color: #111;font-weight: 600;"> <span>医生：{{ item.doctorName || '' }}</span></view>
							<view class="mar_20" style="color: #111;font-weight: 600;">代表：{{ item.mrName || '' }}</view>
							<view style="color: #111;font-weight: 600;" class="mar_20">品种：{{ item.drugName || '' }}</view>
							<view v-if="item.type != 4 && (item.researchObject != 1)" style="color: #111;font-weight: 600;" class="mar_20">科室：{{ item.departmentName || '' }}</view>
						</view>
						<view v-if="current == 1 && item.status != 7 && item.status != 8" class="button_flex">
							<view  @tap.stop="taskPunchFun(item)" class="custom-style" style="margin-right: 10rpx;">
								<u-button 
									type="primary" text="同意"></u-button>
							</view>
							<view @tap.stop="submitTaskFun(item)" class="custom-style">
								<u-button
									type="primary" text="驳回"></u-button>
							</view>
						</view>
						<view v-if="current == 1 && item.status == 7" class="button_flex">
							<view @tap.stop="taskcommitFun(item)" class="custom-style">
								<u-button
									type="primary" text="同意驳回"></u-button>
							</view>
						</view>
						<!-- <view v-if="current == 1" class="button_flex">
							<view class="custom-style" style="margin-right: 10rpx;width: 160rpx;">
									<view v-if="item.status == 2"  @tap.stop="goToRequestFun(item)" class="">
											<u-button
												type="primary" text="申请驳回"></u-button>
									</view>
									<view @tap.stop="" class="" v-else>
										<u-button
										  disabled
											type="primary" text="申请驳回"></u-button>
									</view>
							</view>
						</view> -->
					</view>
					<view style="color: gray;font-size: 23rpx;margin: 10rpx 0;">时间{{ item.startTime }}至{{ item.endTime }}</view>
				</view>
				<view v-if="toTheEnd" class="" style="color: gray;margin: 20rpx 0;text-align: center;font-size: 26rpx;">
					我到底了~
				</view>
			</scroll-view>
			<view>
				<!--  -->
				
			</view>
<!--  -->
			<u-empty style="margin-top: 100rpx;" v-if="!appListMin.length" text="暂无数据" mode="list"></u-empty>
		</view>
		<u-modal v-if="isshow" showCancelButton="true" :content='"是否确认删除"' cancelText="取消" :show="isshow" @confirm="confirm" @cancel="isshow=false" ref="uModal" :asyncClose="true"></u-modal>
		<u-popup v-if="righthow" :show="righthow" mode="right"  @close="close">
			<view style="width: 600rpx;height: 100%;padding: 20rpx 0 0 20rpx;position: relative;">
				<view style="font-weight: 900;margin-top: 100rpx;">
					年
				</view>
				<view class="flex_t">
					<view :class="{
						 'flex_t_item_b': year != item.dictValue,
						 'flex_t_item': year == item.dictValue,
					}" v-for="(item, index) in yearList" @click="yeatFuns(item)">
						{{ item.dictLabel }}
					</view>
				</view>
				<view style="font-weight: 900;margin-top: 20rpx;">
					月
				</view>
				<view class="flex_t">
					<view :class="{
						 'flex_t_item_b': month != item.dictValue,
						 'flex_t_item': month == item.dictValue,
					}" v-for="(item, index) in moonList" @click="moonFuns(item)">
						{{ item.dictLabel }}
					</view>
				</view>
				<view @click="openFun" class="comi" style="position: absolute;bottom: 20rpx;">
					确认
				</view>
			</view>
		</u-popup>
	</view>
</template>
<script>
	import store from '@/store'
	import { getbyuser, medicalRepresentative } from "@/api/login/index.js"
	import { sysUserType, applyAll, delApply, taskCommit, tasRrollback, set_year_time, set_moon_time, agencyRefuse, agencyAudit, taskcommit } from '@/api/apply.js'
	export default {
		data() {
			return {
				toTheEnd: false,
				totals: 0,
				pageNum: 1,
				righthow: false,
				type: '',
				id: '',
				isshow: false,
				appListMin: [],
				appList: [],
				statusbarH: 0,
				status: 1,
				keyword: '',
				ispro: '1',
				list: ['未提交', '已提交', '已审批'],
				current: 0,
				yearList: [],
				moonList: [],
				year: '',
				month: '',
				userType: '',
				status: '',
				statusAll: {
					[1]: '未提交',
					[2]: '已提交',
					[3]: '已同意',
					[4]: '已驳回',
					[5]: '已取消',
					[6]: '未通过',
					[7]: '申请驳回',
					[8]: '代理商同意',
					[9]: '代理商驳回',
				},
				
			}
		},
		// status: this.status,
		// year: this.year,
		// month: this.month
		 
		mounted() {
			uni.getSystemInfo({
					success:(e)=> {
					this.statusbarH = e.statusBarHeight;
				}
			})
		},
		onShow() {
			console.log('store.getters.setdates', store.getters.setdates)
			if (store.getters.setdates) {
				let str = JSON.parse(JSON.stringify(store.getters.setdates))
				this.current = str.current || 0
				this.year = str.year || ''
				this.month = str.month || ''
				store.dispatch('sethtsta', null)
			}
			store.dispatch('setanswerList', null)
			if (JSON.parse(uni.getStorageSync('userInfo')).userType == '02') {
				// 代表
				this.userType = 2
			} else{
				// 代理商
				this.userType = 1
			}
			((JSON.parse(uni.getStorageSync('userInfo')).userType == '02')? medicalRepresentative : getbyuser)(JSON.parse(uni.getStorageSync('userInfo')).userId).then((resmin) => {
				this.status = resmin.data.status
				if((this.userType == 2 && this.status == 6) || (this.userType == 1 && this.status == 4)){
					this.applyAllFun()
					this.getset_year_time()
					this.getset_moon_time()
					store.dispatch('setanswerList', null)
				}
			})
			
		},
		methods: {
			lower(e) {
				if (this.appListMin.length >= this.totals) {
					this.toTheEnd = true
					return
				}
				if (!this.appListMin.length) {
					this.toTheEnd = false
				}
				this.pageNum = this.pageNum+1
				this.applyAllFun('add')
			},
			rightFun() {
				this.righthow = true
			},
			openFun() {
				this.righthow = false
				this.applyAllFun()
			},
			yeatFuns(item) {
				this.year = item.dictValue
			},
			moonFuns(item) {
				this.month = item.dictValue 
			},
			getset_year_time() {
				set_year_time().then((res)=> {
					res.data.unshift({
						dictValue: '',
						dictLabel: '不限'
					})
					this.yearList = res.data
				})
			},
			getset_moon_time() {
				set_moon_time().then((res)=> {
					res.data.unshift({
						dictValue: '',
						dictLabel: '不限'
					})
					this.moonList = res.data
				})
			},
			open() {
				// console.log('open');
			},
			close() {
				this.righthow = false
			},
			confirm() {
				delApply({
					id: this.id,
					type: this.type
				}).then((res) => {
					uni.showToast({
					  title: '删除成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								this.applyAllFun()
								this.isshow = false
							}, 500)
						}
					})
				}).catch(() => {
					this.isshow = false
				})
			},
			// 删除
			delFun(item) {
				this.id = item.id
				this.type = item.type
				this.isshow = true
			},
			applyAllFun(add) {
				if (!add) {
					this.appListMin = []
					this.pageNum = 1
					this.toTheEnd = false
				}
				applyAll({
					status: this.current+1,
					year: this.year,
					month: this.month,
					pageNum: this.pageNum,
					pageSize: 10
				}).then((res) => {
					this.totals = res.total
					this.appListMin = [...this.appListMin, ...res.rows]
				})
			},
			taskPunchFun(item) {
				agencyAudit({
					id: item.id,
					type: item.type
				}).then((res) => {
					uni.showToast({
					  title: '同意成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								this.applyAllFun() //未提交列表
								this.isshow = false
							}, 500)
						}
					})
				})
			},
			goTovixFun(item) {
				if (item.type == 2) {
					let urlAll = ''
					if (item.drugOwner ==1 && item.researchObject == 0) {
						// 自研	医生
						urlAll = '/pages/mytaskManagement/researchDoctor/index'
					} else if (item.drugOwner ==1 && item.researchObject == 1) {
						// 自研患者
						urlAll = '/pages/mytaskManagement/researchPatients/index'
					} else if (item.drugOwner ==2 && item.researchObject == 0) {
						// 竞品	医生
						urlAll = '/pages/mytaskManagement/competingDoctor/index'
					} else {
						// 竞品	患者
						urlAll = '/pages/mytaskManagement/competingPatients/index'
					}
					uni.navigateTo({
						url:urlAll + '?id=' + item.id
					})
					this.setDateFun()
					return
				}
				let urls = {
					[1]: "/pages/mytaskManagement/addVisitTask/index",
					[2]: "/pages/mytaskManagement/researchDoctor/index",
					[4]: "/pages/mytaskManagement/academicConference/index",
					[3]: "/pages/mytaskManagement/departmentMeeting/index",
				}
				uni.navigateTo({
					url: urls[item.type] + '?id=' + item.id
				})
				this.setDateFun()
			},
			proFun(val) {
				this.ispro = val
			},
			sectionChange(index) {
				this.current = index;
				// if (index != 0) {
					// this.status = index
				this.applyAllFun()
			},
			taskcommitFun(item) {
				taskcommit({
					id: item.id,
					type: item.type
				}).then((res) => {
					uni.showToast({
					  title: '已同意驳回',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								this.applyAllFun() //未提交列表
								this.isshow = false
							}, 500)
						}
					})
				})
			},
			submitTaskFun(item) {
				agencyRefuse({
					id: item.id,
					type: item.type
				}).then((res) => {
					uni.showToast({
					  title: '驳回成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								this.applyAllFun() //未提交列表
								this.isshow = false
							}, 500)
						}
					})
				})
			},
			viewTasksFun(type, item) {
				if (type == 0) {
					
					if (item.type == 1) {
						
						uni.navigateTo({
							url: "/pages/mytaskManagement/checkVisitTask/index?id=" + item.id + "&check='1'" + "&looks='1'"
						})
					} else if (item.type == 2) {
						let urlAll = ''
						if (item.drugOwner ==1 && item.researchObject == 0) {
							// 自研	医生
							urlAll = '/pages/mytaskManagement/researchDoctor/index'
						} else if (item.drugOwner ==1 && item.researchObject == 1) {
							// 自研患者
							urlAll = '/pages/mytaskManagement/researchPatients/index'
						} else if (item.drugOwner ==2 && item.researchObject == 0) {
							// 竞品	医生
							urlAll = '/pages/mytaskManagement/competingDoctor/index'
						} else {
							// 竞品	患者
							urlAll = '/pages/mytaskManagement/competingPatients/index'
						}
						uni.navigateTo({
							url:urlAll + '?id=' + item.id + '&check="1"' + "&looks='1'"
						})
					} else if (item.type == 3) {
						uni.navigateTo({
							url: "/pages/mytaskManagement/departmentMeeting/index?id=" + item.id + "&check='1'" + "&looks='1'"
						})
					} else if (item.type == 4) {
						uni.navigateTo({
							url: "/pages/mytaskManagement/academicConference/index?id=" + item.id + "&check='1'" + "&looks='1'"
						})
					}
					this.setDateFun()
				} else if (type == 1 || type == 2) {
					if (item.type == 1) {
						uni.navigateTo({
							url: "/pages/mytaskManagement/checkVisitTask/index?id=" + item.id + "&looks='1'"
						})
					} else if (item.type == 2) {
						let urlAll = ''
						if (item.drugOwner ==1 && item.researchObject == 0) {
							// 自研	医生
							urlAll = '/pages/mytaskManagement/researchDoctor/index'
						} else if (item.drugOwner ==1 && item.researchObject == 1) {
							// 自研患者
							urlAll = '/pages/mytaskManagement/researchPatients/index'
						} else if (item.drugOwner ==2 && item.researchObject == 0) {
							// 竞品	医生
							urlAll = '/pages/mytaskManagement/competingDoctor/index'
						} else {
							// 竞品	患者
							urlAll = '/pages/mytaskManagement/competingPatients/index'
						}
						uni.navigateTo({
							url:urlAll + '?id=' + item.id + '&check="1"&looks="1"'
						})
					} else if (item.type == 3) {
						uni.navigateTo({
							url: "/pages/mytaskManagement/departmentMeeting/index?id=" + item.id + "&check='1'" + "&looks='1'"
						})
					} else if (item.type == 4) {
						uni.navigateTo({
							url: "/pages/mytaskManagement/academicConference/index?id=" + item.id + "&check='1'" + "&looks='1'"
						})
					}
					this.setDateFun()
				}
				// uni.navigateTo({
				// 	url: "/pages/mytaskManagement/viewTasks/index"
				// })
			},
			goToRequestFun(item) {
				tasRrollback({
					id: item.id,
					type: item.type
				}).then((res) => {
					uni.showToast({
					  title: '驳回成功',
					  icon: "none",
					  duration: 2000,
						success: () => {
							setTimeout(() => {
								this.applyAllFun() //未提交列表
								this.isshow = false
							}, 500)
						}
					})
				})
				// uni.navigateTo({
				// 	url: type == 1 ? "/pages/task/research/index" : "/pages/task/taskRequest/index"
				// }) 
			},
			setDateFun() {
				store.dispatch('sethtsta', {
					current: this.current,
					year: this.year,
					month: this.month
				})
			}
		},
		// onUnload() {
		// 	console.log('445566', this.status)
		// 	console.log(this.year)
		// 	console.log(this.month)
		// },
	}
</script>
<style lang="scss" scoped>
	.sea_pad {
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.container {
		background-color: #f7f8ff;
		// background-color: #fff;
		display: flex;
		flex-direction: column;
		height: 100vh;
		overflow: hidden;
	}

	.province {
		flex: 1;
		overflow: hidden;
		padding: 0 20rpx;
		margin-top: 20rpx;
	}

	.pro_list {
		padding: 20rpx 50rpx;
		box-sizing: border-box;
	}

	.pro_list_is {
		background-color: #f7f8ff;
		color: #2979ff;
		position: relative;
	}

	.pro_list_is::before {
		content: '';
		display: block;
		width: 10rpx;
		height: 50rpx;
		background-color: #2979ff;
		position: absolute;
		left: -20rpx;
	}

	.u-search-flex {
		display: flex;
		align-items: center;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.addr_font {
		color: gray;
		font-size: 25rpx;
		margin: 0 10rpx;
		margin-right: 20rpx;
	}

	.chat_left {
		margin-left: 20rpx;
	}

	.items_box {
		background-color: #fff;
		padding: 20rpx;
		border-radius: 20rpx;
		margin-bottom: 20rpx;

		.time_flex {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 24rpx;
			color: gray;

			.mar_20 {
				margin: 15rpx 0;
			}
		}

		.font_w {
			font-weight: 900;
			font-size: 30rpx;
			color: #111;
			display: flex;
			justify-content: space-between;

			span:last-child {
				margin-left: 40rpx;
			}
		}

		.custom-style {
			width: 200rpx;
			border-radius: 100rpx;
			margin: 0;
			overflow: hidden;
		}
	}
	.button_flex {
		display: flex;
		align-items: center;
		.custom-style {
			width: 130rpx;
			margin: 0;
			border-radius: 100rpx;
			overflow: hidden;
		}
	}
	#font_color {
		font-weight: 100;
		color: gray;
		font-size: 24rpx;
	}
	.flex_t {
		display: flex;
		flex-wrap: wrap;
	}
	.flex_t_item {
		width: 150rpx;
		text-align: center;
		padding: 20rpx 0rpx;
		border: 1px solid #2979ff;
		margin-right: 40rpx;
		margin-top: 20rpx;
		color: #2979ff;
		font-size: 26rpx;
		box-sizing: border-box;
	}
	.flex_t_item_b {
		width: 150rpx;
		text-align: center;
		padding: 20rpx 0rpx;
		margin-right: 40rpx;
		margin-top: 20rpx;
		background-color: #f7f7f7;
		font-size: 26rpx;
		box-sizing: border-box;
	}
	.comi {
		padding: 10rpx 120rpx;
		background-color: #2979ff;
		color: white;
		left: 50%;
		transform: translateX(-50%);
		border-radius: 10rpx;
	}
</style>
